import PageBase from "../Base/PageBase";
import SystemBase from "../Base/SystemBase";
import { PAGE } from "../Enum";
import { IPageMap, IPagePramsBase } from "../Interface";
import mainRoot from "../MainRoot";

export default class UISystem extends SystemBase {
    private readonly m_BundleName: string = 'gameBundle';
    private m_loadingPage: PAGE[] = [];
    private get Bundle() {
        return cc.assetManager.getBundle(this.m_BundleName);
    }
    systemName: string = "页面系统";

    private pageMap: IPageMap = <IPageMap>{};

    SystemInit() {
        super.SystemInit();
    }

    async OpenPage(pageName: PAGE, prams?: IPagePramsBase) {
        // this.Log("尝试打开", pageName);
        if (this.pageMap[pageName]) {
            // this.Log("从隐藏打开", pageName);
            this.pageMap[pageName].node.active = true;
            // this.pageMap[pageName].node.zIndex = this.pageMap[pageName].node.parent.childrenCount;
            this.pageMap[pageName].OnPageOpen(prams);
            return;
        }
        this.m_loadingPage.push(pageName);
        return new Promise<PageBase>((resolve, reject) => {
            mainRoot.frameBundle.load(`Page/${pageName}`, async (err, prefab: cc.Prefab) => {
                if (err) {
                    reject(err);
                    console.error("页面加载错误", err);
                    return;
                }
                PageBase.isPlaySound = false;
                // this.Log("加载完成", pageName);
                let page = cc.instantiate(prefab);
                let com = page.getComponent(PageBase);
                if (!com) { this.Warn("页面没有挂载脚本!!!", pageName); resolve(null); return; }
                this.pageMap[pageName] = com;
                com.pageName = pageName;
                // let name : string = 'UILayer';
                mainRoot.UIRoot.addChild(page);
                await com.OnPageOpen(prams);
                let index = this.m_loadingPage.indexOf(pageName);
                if (index > -1) {
                    this.m_loadingPage.splice(index);
                }

                resolve(com);
            });
        });
    }

    HidePage(pageName: PAGE) {
        if (this.pageMap[pageName]) {
            this.pageMap[pageName].HidePage();
            // this.pageMap[pageName].OnPageClose();
        }
    }
    ShowPage(pageName) {
        if (this.pageMap[pageName]) {
            this.pageMap[pageName].ShowPage();
            // this.pageMap[pageName].OnPageClose();
        }
    }

    ClosePage(pageName: PAGE) {
        if (this.pageMap[pageName]) {
            this.pageMap[pageName].OnPageClose();
            this.pageMap[pageName] = null;
        }
    }

    CloseAllPage() {
        for (const key in this.pageMap) {
            if (Object.prototype.hasOwnProperty.call(this.pageMap, key)) {
                const element = this.pageMap[key];

                if (element) {
                    element.OnPageClose();
                }
            }
        }
        this.pageMap = {};
    }
}